namespace NAct.Patterns
{
	using System.Collections.Generic;
	using System.Text.RegularExpressions;

	public class PatternParseResults: IPatternParseResults
	{
		private readonly IDictionary<string, string> matches = new Dictionary<string, string>();
		private readonly string source;

		public PatternParseResults( MatchCollection collection, IEnumerable<string> groupNames )
		{
			foreach ( Match match in collection )
			{
				foreach ( string name in groupNames )
				{
					if ( name == "0" )
					{
						source = match.Groups[ name ].Value;
					}
					else
					{
						matches.Add( name, match.Groups[ name ].Value );
					}
				}
			}
		}

		public int Count
		{
			get { return matches.Count; }
		}

		public string Source
		{
			get { return source; }
		}

		public string Get( string tokenName )
		{
			if ( matches.ContainsKey( tokenName ) )
			{
				return matches[ tokenName ];
			}

			throw new NactException(
				string.Format(
					"Could not match parameter \"{0}\" to a match group - ensure the match groups have the same name as the parameters of the method they decorate",
					tokenName ) );
		}
	}
}